package com.hibase.core.bireport.web.bi;

import com.github.pagehelper.PageInfo;
import com.hibase.common.entity.ResponseModel;
import com.hibase.common.util.EntityUtil;
import com.hibase.core.bireport.entity.BiReportSearchVo;
import com.hibase.core.bireport.entity.bi.WmsBiConfig;
import com.hibase.core.bireport.entity.bi.vo.*;
import com.hibase.core.bireport.service.bi.BiConfigService;
import com.hibase.core.bireport.service.bi.BiService;
import com.hibase.core.web.annotation.HibasePostRequestMapping;
import com.hibase.core.web.entity.OperationIdEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chenfeng
 * @since 2021-11-08
 */
@RestController
@RequestMapping("/wms-bi-config")
@Api(tags = "fineReport报表")
@Slf4j
public class WmsBiConfigController {


    @Autowired
    private BiConfigService wmsBiConfigService;

    @Autowired
    private BiService biService;

    @ApiOperation(value = "查询列表", httpMethod = "POST")
    @HibasePostRequestMapping(value = "selectWmsBiConfigList")
    public ResponseModel<WmsBiConfigListVO> selectWmsBiConfigList(@RequestBody WmsBiConfigSearchVO searchVO) {

        PageInfo result = wmsBiConfigService.selectWmsBiConfigList(searchVO);

        result.setList(EntityUtil.transform(result.getList(), WmsBiConfigListVO.class));

        return ResponseModel.successMsg(result);
    }

    @ApiOperation(value = "新增记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "insertWmsBiConfig")
    public ResponseModel<String> insertWmsBiConfig(@RequestBody WmsBiConfigInsertVO insertVO) {

        WmsBiConfig entity = EntityUtil.transform(insertVO, WmsBiConfig.class);

        wmsBiConfigService.saveWmsBiConfig(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "修改记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "updateWmsBiConfig")
    public ResponseModel<String> updateWmsBiConfig(@RequestBody WmsBiConfigUpdateVO updateVO) {

        WmsBiConfig entity = EntityUtil.transform(updateVO, WmsBiConfig.class);

        wmsBiConfigService.saveWmsBiConfig(entity);

        return ResponseModel.successMsg(entity.getId());
    }

    @ApiOperation(value = "查询详情", httpMethod = "POST")
    @HibasePostRequestMapping(value = "viewWmsBiConfigDetails")
    public ResponseModel<WmsBiConfigViewVO> viewWmsBiConfigDetails(@RequestBody OperationIdEntity operationidEntity) {

        return ResponseModel.successMsg(EntityUtil.transform(wmsBiConfigService.viewWmsBiConfig(operationidEntity.getId()), WmsBiConfigViewVO.class));
    }

    @ApiOperation(value = "删除记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/deleteWmsBiConfig")
    public ResponseModel<String> deleteWmsBiConfig(@RequestBody OperationIdEntity delete) {

        wmsBiConfigService.deleteWmsBiConfig(delete.getId());

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "导出记录", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/exportWmsBiConfig")
    public ResponseModel<String> exportWmsBiConfig(@RequestBody WmsBiConfigSearchVO searchVO, HttpServletResponse response) {

        wmsBiConfigService.exportWmsBiConfig(searchVO, response);

        return ResponseModel.successMsg();
    }

    @ApiOperation(value = "获得报表url", httpMethod = "POST")
    @HibasePostRequestMapping(value = "/getReportUrl")
    public ResponseModel<String> getReportUrl(@RequestBody @Validated BiReportSearchVo searchVO) {
        String url = biService.getReportUrl(searchVO);
        return ResponseModel.successMsg(url);
    }
}
